import $getQuery from "./$getQuery";
import { useNavigate } from "react-router-dom";

export default (backUrl?: string) => {
    const navigator = useNavigate()
    if (!history) return;

    setTimeout(() => {
        const $params = $getQuery()
        // const { query } = history.location;
        const { redirect } = $params as { redirect: string; };

        if (redirect) {
            const str = window.decodeURIComponent(redirect);
            if (new RegExp('^http').test(str)) {
                window.location.replace(str);
                return;
            }
        } else {
            if (backUrl) {
                window.location.replace(backUrl);
                return;
            }
        }
        // navigator(redirect || '/');
        window.location.replace(redirect || '/') // 不会保存历史记录
    }, 10);
};